﻿@using Huirui.Cavan.Domain.MainBoundedContext.SystemModule
@model Huirui.Cavan.Presentation.ViewModel.SystemModule.ICurrencyViewModel
@{Layout = null;}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.CURRENCY))
{
    <div class="tool_bar">
        <input id="currency_save" type="submit" style="display: none;" />
        <input id="currency_reset" type="reset" style="display: none" />
        <a href="#" id="save_currency" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'"
            onclick="javascript:$('#currency_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" id="currencyReset">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'"
            onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label class="BlueRequired">
                    代码:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Code, new { id = "currency_code" })
                @Html.HiddenFor(m => m.Item.Id, new { id = "currency_id" })
            </td>
            <td class="c100">
                <label class="BlueRequired">
                    名称:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Name, new { id = "currency_name" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label class="BlueRequired">
                    汇率:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Rate, new { id = "currency_rate" })
            </td>
            <td class="c100">
                <label>
                    是否本位币:</label>
            </td>
            <td class="c160">
                @Html.CheckBoxFor(m => m.Item.IsLocal, new { id = "currency_islocal" })
            </td>
        </tr>
    </table>
}
<table id="currency_dg">
    <thead>
        <tr>
            <th field="ck" checkbox="true"></th>
            <th field="Code" width="80">代码
            </th>
            <th field="Name" width="80">名称
            </th>
            <th field="Rate" width="80">汇率
            </th>
            <th field="StrIsLocal" width="80">是否本位币
            </th>
        </tr>
    </thead>
</table>
<div id="currencytb">
    <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" id="deleteCurrency">批量删除</a>
    <a href="#" class="easyui-linkbutton" iconcls="icon-save" plain="true" onclick="javascript:alert('引出')">引出</a>
</div>
<script type="text/javascript">
    $(function ()
    {
        $("form[action^='/currency/add']").submit(function ()
        {
            $("#currency_code").attr("disabled", false);
            $('a#save_currency').linkbutton('disable');
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data)
                {
                    if (data.IsSuccess)
                    {
                        $('#currency_dg').datagrid('reload');
                    }
                    else
                    {
                        $.messager.alert("提示", data.Messages[0]);
                    }
                    $("#currencyReset").click();
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })
            $('a#save_currency').linkbutton('enable');
            return false;
        });

        $('#currency_dg').datagrid({
            title: "货币列表",
            singleSelect: true,
            idfield: "ID",
            toolbar: '#currencytb',
            url: "/currency/list",
            onClickRow: function (rowIndex, rowData)
            {
                $("#currency_id").val(rowData.Id);
                $("#currency_code").val(rowData.Code).attr("disabled", true);
                $("#currency_name").val(rowData.Name);
                $("#currency_rate").val(rowData.Rate);
                if (rowData.IsLocal)
                {
                    $("#currency_islocal").attr("checked", "checked");
                }
                else
                {
                    $("#currency_islocal").removeAttr("checked");
                }
            }
        });

        $("#currency_islocal").bind("click", function ()
        {
            if ($(this).attr("checked") == "checked")
            {
                var rows = $('#currency_dg').datagrid('getRows');
                for (var i = 0; i < rows.length; i++)
                {
                    if (rows[i].StrIsLocal.toString() == "是")
                    {
                        alert("只能有一个本位币");
                        $(this).removeAttr("checked");
                        return;
                    }
                }
            }
        })


        $("#deleteCurrency").bind("click", function ()
        {
            if ($('#currency_dg').datagrid('getChecked').length)
            {
                var selected = $('#currency_dg').datagrid('getChecked');

                var effectRow = new Object();
                if (selected.length)
                {
                    effectRow["selected"] = JSON.stringify(selected);
                }

                $.post("/currency/delete", effectRow, function (data)
                {
                    if (data.IsSuccess)
                    {
                        $('#currency_dg').datagrid('reload');
                        $('#currency_dg').datagrid('clearChecked');
                    }
                }, "JSON").error(function ()
                {
                    $.messager.alert("提示", "网络异常，请稍候再试");
                });
            }
        });

        $("#currencyReset").bind("click", function ()
        {
            $("#currency_code").attr("disabled", false);
            $("#currency_id").val("");
            $("#currency_reset").click();
            $("#currency_islocal").removeAttr("checked");
        });
    });
</script>
